const Mock = require('mockjs')

const inventoryList = []
const count = 100

for (let i = 0; i < count; i++) {
  const goodsId = Mock.Random.integer(1000, 1100)
  const machineId = Mock.Random.integer(2000, 2100)

  inventoryList.push(Mock.mock({
    goodsId: goodsId,
    goodsName: Mock.Random.ctitle(3, 8),
    machineId: machineId,
    machineName: Mock.Random.ctitle(3, 8) + '贩卖机',
    currentStock: Mock.Random.integer(0, 20),
    warningStock: 5,
    maxStock: 20,
    status: function () {
      if (this.currentStock === 0) return 'out'
      if (this.currentStock <= this.warningStock) return 'warning'
      return 'normal'
    },
    lastReplenishTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'),
    lastReplenishOperator: Mock.Random.cname()
  }))
}

module.exports = [
  // 获取库存列表
  {
    url: '/api/inventory/list',
    type: 'get',
    response: config => {
      const { goodsName, machineName, status, pageNum = 1, pageSize = 10 } = config.query
      let filteredList = [...inventoryList].filter(item => {
        if (goodsName && item.goodsName.indexOf(goodsName) < 0) return false
        if (machineName && item.machineName.indexOf(machineName) < 0) return false
        if (status && item.status !== status) return false
        return true
      })

      const pageList = filteredList.slice((pageNum - 1) * pageSize, pageNum * pageSize)

      return {
        code: 200,
        message: 'success',
        data: {
          list: pageList,
          total: filteredList.length
        }
      }
    }
  },

  // 补货操作
  {
    url: '/api/inventory/replenish',
    type: 'post',
    response: config => {
      const params = config.body

      if (params.goodsMachines) {
        // 批量补货
        params.goodsMachines.forEach(pair => {
          const item = inventoryList.find(item =>
            item.goodsId === pair.goodsId && item.machineId === pair.machineId
          )
          if (item) {
            item.currentStock += params.quantity
            item.lastReplenishTime = new Date().toISOString()
            item.lastReplenishOperator = params.operator
            item.status = item.currentStock === 0 ? 'out' :
              item.currentStock <= item.warningStock ? 'warning' : 'normal'
          }
        })
      } else {
        // 单个补货
        const item = inventoryList.find(item =>
          item.goodsId === params.goodsId && item.machineId === params.machineId
        )
        if (item) {
          item.currentStock += params.quantity
          item.lastReplenishTime = new Date().toISOString()
          item.lastReplenishOperator = params.operator
          item.status = item.currentStock === 0 ? 'out' :
            item.currentStock <= item.warningStock ? 'warning' : 'normal'
        }
      }

      return {
        code: 200,
        message: '补货成功'
      }
    }
  },

  // 获取库存详情（含补货历史）
  {
    url: '/api/inventory/detail',
    type: 'get',
    response: config => {
      const url = new URL(`http://dummy.com${config.url}`)
      const goodsId = url.searchParams.get('goodsId')
      const machineId = url.searchParams.get('machineId')

      const item = inventoryList.find(item =>
        item.goodsId == goodsId && item.machineId == machineId
      )

      if (!item) {
        return {
          code: 404,
          message: '库存记录不存在'
        }
      }

      // 生成补货历史
      const replenishHistory = []
      const count = Mock.Random.integer(1, 10)
      for (let i = 0; i < count; i++) {
        replenishHistory.push({
          time: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'),
          operator: Mock.Random.cname(),
          quantity: Mock.Random.integer(1, 10),
          remark: Mock.Random.csentence(3, 10)
        })
      }

      return {
        code: 200,
        message: 'success',
        data: {
          replenishHistory: replenishHistory
        }
      }
    }
  }
]